﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OAclient.Models;
using ServiceApplication.Service.Basic;
using CommonLibrary.DataModel.Basic;
using CommonLibrary.Common;
using OAclient.Common;
using System.Data;
using OAclient.Filters;

namespace OAclient.Controllers
{
    [VaildateLogin]
    [ExceptionFilter]
    [LoggerFilter]
    public class TxlController : Controller
    {
        //
        // GET: /Txl/通讯录
        BasicService bs = new BasicService();
        int PageSize = 15;

        public ActionResult TxlMrg()
        {
            return View();
        }
        //workarea-3-5 
        public ActionResult SelfTxlMrg(UserModel user, string txlgrouptype)
        {
            System.Data.DataTable dt_txlgroup = null;
            System.Data.DataTable dt_txl = null;
            if (txlgrouptype == "我的通讯录")
            {

                dt_txlgroup = bs.QueryTxlGroupByUserId(user.user.user_id);
                dt_txl = bs.QueryTxlForFuzzy(user.user.user_id, "");
            }
            else if (txlgrouptype == "共享通讯录")
            {
                dt_txlgroup = bs.QueryTxlGroupByUserId(1);//1号默认为管理员
                dt_txl = bs.QueryTxlForFuzzy(1, "");
            }
            else if (string.IsNullOrEmpty(txlgrouptype) || txlgrouptype == "公司通讯录")
            {
                txlgrouptype = "公司通讯录";
                dt_txlgroup = bs.QueryAllDepts();
                dt_txlgroup.Columns["部门编码"].ColumnName = "小组编号";
                dt_txlgroup.Columns["部门名称"].ColumnName = "名称";

                dt_txl = bs.QueryAllStaff("在职");
                dt_txl.Columns["员工编码"].ColumnName = "编号";
                dt_txl.Columns["员工名称"].ColumnName = "名称";
                dt_txl.Columns["手机"].ColumnName = "手机";
                dt_txl.Columns["办公电话"].ColumnName = "固定电话";
                dt_txl.Columns["办公地址"].ColumnName = "联系地址";
                dt_txl.Columns["qq"].ColumnName = "QQ";
                dt_txl.Columns["传真"].ColumnName = "传真";
                dt_txl.Columns["部门名称"].ColumnName = "部门";
                dt_txl.Columns["职位名称"].ColumnName = "职位";
                dt_txl.Columns["部门编码"].ColumnName = "小组编号";
            }


            List<SelectListItem> list_txlgrouptype = new List<SelectListItem>();
            list_txlgrouptype.Add(new SelectListItem
            {
                Selected = "我的通讯录" == txlgrouptype,
                Text = "我的通讯录",
                Value = "我的通讯录",
            });
            list_txlgrouptype.Add(new SelectListItem
            {
                Selected = "共享通讯录" == txlgrouptype,
                Text = "共享通讯录",
                Value = "共享通讯录",
            });
            list_txlgrouptype.Add(new SelectListItem
            {
                Selected = "公司通讯录" == txlgrouptype,
                Text = "公司通讯录",
                Value = "公司通讯录",
            });
            List<SelectListItem> list_searchtxlgrouptype = new List<SelectListItem>(list_txlgrouptype.ToArray());

            list_searchtxlgrouptype.Add(new SelectListItem
            {
                Selected = false,
                Text = "全部",
                Value = "全部",
            });
            ViewData["list_txlgrouptype"] = list_txlgrouptype;
            ViewData["list_searchtxlgrouptype"] = list_txlgrouptype;
            ViewData["dt_txlgroup"] = dt_txlgroup;
            ViewData["dt_txl"] = dt_txl;
            ViewData["txlgrouptype"] = txlgrouptype;
            return View();
        }
        /// <summary>
        ///  workarea-3-5-1
        ///  我的通讯录（员工）//内容显示小组列表         
        /// </summary>
        /// <param name="user"></param>
        /// <param name="txlgrouptype">我的通讯录，共享通讯录，公司通讯录</param>
        /// <returns></returns>
        public ActionResult TxlGroupTable(UserModel user, string txlgrouptype, int? page)
        {
            System.Data.DataTable dt_txlgroup = null;
            if (string.IsNullOrEmpty(txlgrouptype) || txlgrouptype == "我的通讯录")
            {
                txlgrouptype = "我的通讯录";
                dt_txlgroup = bs.QueryTxlGroupByUserId(user.user.user_id);
            }
            else if (txlgrouptype == "共享通讯录")
            {
                dt_txlgroup = bs.QueryTxlGroupByUserId(1);
            }
            else if (txlgrouptype == "公司通讯录")
            {
                dt_txlgroup = bs.QueryAllDepts();
                dt_txlgroup.Columns["部门编码"].ColumnName = "小组编号";
                dt_txlgroup.Columns["部门名称"].ColumnName = "名称";
            }
            if (page == null)
                page = 1;
            var pageInfo = new PagingInfo
            {
                CurrentPage = page.Value,
                ItemsPerPage = 10,
                TotalItems = dt_txlgroup.Rows.Count
            };

            ViewData["pageInfo"] = pageInfo;
            if (dt_txlgroup.Rows.Count > 0)
                dt_txlgroup = dt_txlgroup.AsEnumerable().Skip((page.Value - 1) * PageSize).Take(PageSize).CopyToDataTable<DataRow>();

            ViewData["txlgrouptype"] = txlgrouptype;
            ViewData["dt_txlgroup"] = dt_txlgroup;
            return View();
        }

        //workarea-3-5-2
        /// <summary>
        /// 查询返回txl列表
        /// </summary>
        /// <param name="?"></param>
        /// <param name="page"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public ActionResult TxlTable(UserModel user, int? page)
        {
            System.Data.DataTable dt_txl = null;
            string searchname = Request["searchname_txl"] ?? Request.QueryString["sname"];
            string searchgrouptype_txl = Request["searchgrouptype_txl"] ?? Request.QueryString["stype"];
            ViewData["sname"] = searchname ?? "";
            ViewData["stype"] = searchgrouptype_txl ?? "";

            if (searchgrouptype_txl == "我的通讯录")
            {
                dt_txl = bs.QueryTxlForFuzzy(user.user.user_id, searchname);
                dt_txl.Columns.Add(new DataColumn("小组类型") { DefaultValue = "我的通讯录" });
            }
            else if (searchgrouptype_txl == "共享通讯录")
            {
                dt_txl = bs.QueryTxlForFuzzy(1, searchname);
                dt_txl.Columns.Add(new DataColumn("小组类型") { DefaultValue = "共享通讯录" });
            }
            else if (searchgrouptype_txl == "公司通讯录")
            {
                dt_txl = bs.QueryStaffByLikeName(searchname);
                dt_txl.Columns.Add(new DataColumn("小组类型") { DefaultValue = "公司通讯录" });
                dt_txl.Columns["员工编码"].ColumnName = "编号";
                dt_txl.Columns["员工名称"].ColumnName = "名称";
                dt_txl.Columns["手机"].ColumnName = "手机";
                dt_txl.Columns["办公电话"].ColumnName = "固定电话";
                dt_txl.Columns["办公地址"].ColumnName = "联系地址";
                dt_txl.Columns["qq"].ColumnName = "QQ";
                dt_txl.Columns["传真"].ColumnName = "传真";
                dt_txl.Columns["部门名称"].ColumnName = "部门";
                dt_txl.Columns["职位名称"].ColumnName = "职位";
                dt_txl.Columns["部门编码"].ColumnName = "小组编号";

            }
            else if (searchgrouptype_txl == "全部")
            {
                dt_txl = bs.QueryTxlForFuzzy(user.user.user_id, searchname);
                dt_txl.Columns.Add(new DataColumn("小组类型") { DefaultValue = "我的通讯录" });
                var dt_share = bs.QueryTxlForFuzzy(1, searchname);
                dt_share.Columns.Add(new DataColumn("小组类型") { DefaultValue = "共享通讯录" });
                foreach (DataRow item in dt_share.Rows)
                {
                    dt_txl.ImportRow(item);
                }

                var dt_staff = bs.QueryStaffByLikeName(searchname);
                foreach (DataRow item in dt_staff.Rows)
                {
                    DataRow dr = dt_txl.NewRow();
                    dr["编号"] = item["员工编码"].ToString();
                    dr["名称"] = item["员工名称"].ToString();
                    dr["手机"] = item["手机"].ToString();
                    dr["固定电话"] = item["办公电话"].ToString();
                    dr["联系地址"] = item["办公地址"].ToString();
                    dr["QQ"] = item["qq"].ToString();
                    dr["传真"] = item["传真"].ToString();
                    dr["部门"] = item["部门名称"].ToString();
                    dr["职位"] = item["职位名称"].ToString();
                    dr["小组编号"] = item["部门编码"].ToString();
                    dr["小组类型"] = "公司通讯录";
                    dt_txl.Rows.Add(dr);
                }
            }


            if (page == null)
                page = 1;
            var pageInfo = new PagingInfo
            {
                CurrentPage = page.Value,
                ItemsPerPage = 10,
                TotalItems = dt_txl.Rows.Count
            };
            ViewData["pageInfo"] = pageInfo;
            if (dt_txl.Rows.Count > 0)
                dt_txl = dt_txl.AsEnumerable().Skip((page.Value - 1) * PageSize).Take(PageSize).CopyToDataTable<DataRow>();

            ViewData["dt_txl"] = dt_txl;
            ViewData["txlgrouptype"] = searchgrouptype_txl;
            return View();
        }


        /// <summary>
        ///         //workarea-3-5-3
        /// </summary>
        /// <param name="txl_id">txl_id 或 user_id :如果该参数有值，则说明不是添加模式</param>
        /// <param name="txlgrouptype"></param>
        /// <returns></returns>
        public ActionResult TxlAddOrUpdate(string txl_id, string txlgroup_id, string txlgrouptype, UserModel user)
        {

            if (!string.IsNullOrEmpty(txl_id))
            {
                if (string.IsNullOrEmpty(txlgrouptype) || txlgrouptype == "我的通讯录" || txlgrouptype == "共享通讯录")
                {
                    var txlEntity = bs.QueryTxl(int.Parse(txl_id));
                    if (txlEntity != null)
                    {
                        ViewData["txl_address"] = txlEntity.txl_address;
                        ViewData["txl_cellphone"] = txlEntity.txl_cellphone;
                        ViewData["txl_dept"] = txlEntity.txl_dept;
                        ViewData["txl_descn"] = txlEntity.txl_descn;
                        ViewData["txl_duty"] = txlEntity.txl_duty;
                        ViewData["txl_email"] = txlEntity.txl_email;
                        ViewData["txl_fax"] = txlEntity.txl_fax;
                        ViewData["txl_id"] = txlEntity.txl_id;
                        ViewData["txl_name"] = txlEntity.txl_name;
                        ViewData["txl_QQ"] = txlEntity.txl_QQ;
                        ViewData["txl_telephone"] = txlEntity.txl_telephone;
                        ViewData["txlgroup_id"] = txlEntity.txlgroup_id;
                        //ViewData["user_id"] = txlEntity.user_id;

                        if (txlgrouptype == "共享通讯录")
                        {
                            var txlgroup = bs.QueryTxlGroup(int.Parse(txlgroup_id));
                            List<SelectListItem> txlgrouplist = new List<SelectListItem>();

                            txlgrouplist.Add(new SelectListItem
                            {
                                Selected = true,
                                Text = txlgroup.txlgroup_name,
                                Value = txlgroup_id,
                            });


                            ViewData["txlgrouplist"] = txlgrouplist;
                        }
                        else
                        {
                            var dt_txlgroup = bs.QueryTxlGroupByUserId(user.user.user_id);
                            List<SelectListItem> txlgrouplist = new List<SelectListItem>();
                            for (int i = 0; i < dt_txlgroup.Rows.Count; i++)
                            {
                                txlgrouplist.Add(new SelectListItem
                                {
                                    Selected = txlgroup_id == dt_txlgroup.Rows[i]["小组编号"].ToString(),
                                    Text = dt_txlgroup.Rows[i]["名称"].ToString(),
                                    Value = dt_txlgroup.Rows[i]["小组编号"].ToString()
                                });
                            }

                            ViewData["txlgrouplist"] = txlgrouplist;
                        }
                    }
                }
                else if (txlgrouptype == "公司通讯录")
                {
                    var path = "../../webstore/Person/";
                    var se = bs.QueryStaff(int.Parse(txl_id));
                    if (se != null)
                    {
                        var dutyEntity = bs.QueryDuty(se.dept_id);
                        var deptEntity = bs.QueryDept(se.duty_id);

                        ViewData["txl_address"] = se.staff_officeaddr; ;
                        ViewData["txl_cellphone"] = se.staff_mobile;
                        ViewData["txl_dept"] = deptEntity.dept_name;
                        ViewData["txl_descn"] = se.staff_descn;
                        ViewData["txl_duty"] = dutyEntity.duty_name;
                        ViewData["txl_email"] = se.staff_email;
                        ViewData["txl_fax"] = se.staff_fax;
                        ViewData["txl_id"] = "";
                        ViewData["txl_name"] = se.staff_name;
                        ViewData["txl_QQ"] = se.staff_qq;
                        ViewData["txl_telephone"] = se.staff_officenum;
                        ViewData["txlgroup_id"] = "";
                        ViewData["txl_img"] = path + se.staff_img;
                        //ViewData["user_id"] = userEntity.user_id;

                        List<SelectListItem> txlgrouplist = new List<SelectListItem>();

                        txlgrouplist.Add(new SelectListItem
                        {
                            Selected = true,
                            Text = deptEntity.dept_name,
                            Value = deptEntity.dept_id.ToString(),
                        });

                        ViewData["txlgrouplist"] = txlgrouplist;
                    }
                }

                if (txlgrouptype == "我的通讯录")
                {
                    ViewData["type"] = "update";
                }
                else
                {
                    ViewData["type"] = "show";
                }
            }
            else
            {
                ViewData["txl_address"] = "";
                ViewData["txl_cellphone"] = "";
                ViewData["txl_dept"] = "";
                ViewData["txl_descn"] = "";
                ViewData["txl_duty"] = "";
                ViewData["txl_email"] = "";
                ViewData["txl_fax"] = "";
                ViewData["txl_id"] = "";
                ViewData["txl_name"] = "";
                ViewData["txl_QQ"] = "";
                ViewData["txl_telephone"] = "";

                var dt_txlgroup = bs.QueryTxlGroupByUserId(user.user.user_id);
                if (dt_txlgroup.Rows.Count == 0)
                {
                    ViewData["txlgroup_id"] = "";
                    ViewData["txlgrouplist"] = null;
                }
                else
                {
                    //var default_txlgroupname = dt_txlgroup.Rows[0]["名称"].ToString();
                    var default_txlgroupid = dt_txlgroup.Rows[0]["小组编号"].ToString();
                    List<SelectListItem> txlgrouplist = new List<SelectListItem>();
                    for (int i = 0; i < dt_txlgroup.Rows.Count; i++)
                    {
                        txlgrouplist.Add(new SelectListItem
                        {
                            Selected = default_txlgroupid == dt_txlgroup.Rows[i]["小组编号"].ToString(),
                            Text = dt_txlgroup.Rows[i]["名称"].ToString(),
                            Value = dt_txlgroup.Rows[i]["小组编号"].ToString()
                        });
                    }
                    ViewData["txlgroup_id"] = default_txlgroupid;
                    ViewData["txlgrouplist"] = txlgrouplist;
                }

                ViewData["type"] = "add";
            }

            //ViewData["txlgroup_id"] = string.IsNullOrEmpty(txlgroup_id) ? "" : txlgroup_id;
            return View();
        }


        #region 方法

        public ActionResult DoTxlGroupAdd(string group_name, UserModel user)
        {
            var txlGroupEntity = bs.QueryTxlGroup(user.user.user_id.ToString(), group_name);
            if (null != txlGroupEntity)
            {
                return Json(new ResTag(false, "该小组已存在"));
            }
            else
            {
                txlGroupEntity = new TxlGroupEntity()
                {
                    txlgroup_descn = string.Empty,
                    txlgroup_name = group_name,
                    user_id = user.user.user_id,
                };

                bs.AddTxlGroup(txlGroupEntity);
            }
            return Json(new ResTag(true));
        }

        public ActionResult DoTxlGroupDel(string txlgroup_id, UserModel user)
        {
            var dt_txl = bs.QueryTxlByTxlGroupId(int.Parse(txlgroup_id));
            if (0 != dt_txl.Rows.Count)
            {
                return Json(new ResTag(false, "请先删除所有组员才允许删除该组"));
            }
            else
            {
                bs.DelTxlGroup(int.Parse(txlgroup_id));
            }
            return Json(new ResTag(true));
        }

        public ActionResult DoTxlGroupUpdate(string txlgroup_id, string txlgroup_name, UserModel user)
        {
            var txlGroupEntity = bs.QueryTxlGroup(int.Parse(txlgroup_id));
            if (null == txlGroupEntity)
            {
                return Json(new ResTag(false, "该小组已不存在"));
            }
            else
            {
                txlGroupEntity.txlgroup_name = txlgroup_name;

                bs.UpdateTxlGroup(txlGroupEntity);
            }
            return Json(new ResTag(true));
        }

        public ActionResult DoTxlAdd(UserModel user)
        {
            var tmp_txl = bs.QueryTxl(user.user.user_id, Request["txl_name"].ToString());
            var txlgroup_id = Request["txlgroup_id"];
            if (null != tmp_txl) return Json(new ResTag(false, tmp_txl.txl_name + "已存在"));
            if (string.IsNullOrEmpty(txlgroup_id)) return Json(new ResTag(false, "请选择小组"));

            TxlEntity txlEntity = new TxlEntity()
            {
                txl_name = Request["txl_name"],
                txl_cellphone = Request["txl_cellphone"],
                txl_telephone = Request["txl_telephone"],
                txl_address = Request["txl_address"],
                txl_email = Request["txl_email"],
                txl_QQ = Request["txl_QQ"],
                txl_fax = Request["txl_fax"],
                txl_dept = Request["txl_dept"],
                txl_duty = Request["txl_duty"],
                txl_descn = Request["txl_descn"],
                txlgroup_id = int.Parse(txlgroup_id),
                user_id = user.user.user_id,
            };

            bs.AddTxl(txlEntity);
            return Json(new ResTag(true));
        }

        public ActionResult DoTxlUpdate(UserModel user)
        {
            TxlEntity txlEntity = bs.QueryTxl(int.Parse(Request["txl_id"]));
            if (txlEntity == null)
                return Json(new ResTag(false, "更新失败"));
            txlEntity.txl_name = Request["txl_name"];
            txlEntity.txl_cellphone = Request["txl_cellphone"];
            txlEntity.txl_telephone = Request["txl_telephone"];
            txlEntity.txl_address = Request["txl_address"];
            txlEntity.txl_email = Request["txl_email"];
            txlEntity.txl_QQ = Request["txl_QQ"];
            txlEntity.txl_fax = Request["txl_fax"];
            txlEntity.txl_dept = Request["txl_dept"];
            txlEntity.txl_duty = Request["txl_duty"];
            txlEntity.txl_descn = Request["txl_descn"];
            txlEntity.txlgroup_id = int.Parse(Request["txlgroup_id"].ToString());

            bs.UpdateTxl(txlEntity);
            return Json(new ResTag(true));
        }

        public ActionResult DoTxlDel(string txl_id)
        {
            bs.DelTxl(int.Parse(txl_id));

            return Json(new ResTag(true));
        }


        public ActionResult DoGetTxlByTxlId(string id, string type)
        {

            if (string.IsNullOrEmpty(type) || type == "1")
            {
                TxlEntity txlEntity = bs.QueryTxl(int.Parse(id));
                if (txlEntity != null)
                {
                    var Data = new
                    {
                        txl_id = txlEntity.txl_id,
                        txl_name = txlEntity.txl_name,
                        txl_cellphone = txlEntity.txl_cellphone,
                        txl_telephone = txlEntity.txl_telephone,
                        txl_address = txlEntity.txl_address,
                        txl_email = txlEntity.txl_email,
                        txl_QQ = txlEntity.txl_QQ,
                        txl_fax = txlEntity.txl_fax,
                        txl_dept = txlEntity.txl_dept,
                        txl_duty = txlEntity.txl_duty,
                        txl_descn = txlEntity.txl_descn,
                    };
                    return Json(Data);
                }
            }
            else
            {
                UserEntity userEntity = bs.QueryUser(int.Parse(id));
                StaffEntity se = bs.QueryStaff(userEntity.staff_id);
                var deptEntity = bs.QueryDept(se.dept_id);
                var dutyEntity = bs.QueryDuty(se.duty_id);
                if (userEntity != null)
                {
                    var Data = new
                    {
                        txl_id = userEntity.user_id,
                        txl_name = se.staff_name,
                        txl_cellphone = se.staff_mobile,
                        txl_telephone = se.staff_officenum,
                        txl_address = se.staff_liveaddr,
                        txl_email = se.staff_email,
                        txl_QQ = se.staff_qq,
                        txl_fax = se.staff_fax,
                        txl_dept = deptEntity.dept_name,
                        txl_duty = dutyEntity.duty_name,
                        txl_descn = "",
                    };
                    return Json(Data);
                }

            }

            return Json("");

        }
        #endregion
    }
}
